package com.grasp.db;

import java.util.ArrayList;
import java.util.List;

import com.grasp.base.Logger;
import com.grasp.data.IconItem;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 *场景表
 */
public class SenceTable implements SQLiteInterface {

	private static final int COLUMN0 = 0;
	private static final int COLUMN1 = 1;
	private static final int COLUMN2 = 2;
	private static final int COLUMN3 = 3;

	private MySQLiteHelper mMySQLiteHelper;
	private SQLiteDatabase mSQLiteDatabase;

	public static final String TABLE_ID = "_id";
	public static final String SENCE_TABLE = "SenceTable";
	private static final String SENCE_NAME = "sence_name";
	private static final String SENCE_ICON_NAME = "icon_name";
	private static final String SENCE_DESC = "_describe";
	public static final String SENCE_COLUMNS[] = { TABLE_ID, SENCE_NAME,
			SENCE_ICON_NAME, SENCE_DESC };

	public SenceTable(Context context) {
		mMySQLiteHelper = new MySQLiteHelper(context, this);
		mSQLiteDatabase = mMySQLiteHelper.getWritableDatabase();
	}

	@Override
	public void createTable(SQLiteDatabase db) {
		String senceSql = "CREATE TABLE IF NOT EXISTS " + SENCE_TABLE + " ("
				+ SENCE_COLUMNS[COLUMN0]
				+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
				+ SENCE_COLUMNS[COLUMN1] + " VARCHAR, "
				+ SENCE_COLUMNS[COLUMN2] + " VARCHAR, "
				+ SENCE_COLUMNS[COLUMN3] + " VARCHAR);";
		db.execSQL(senceSql);
		Logger.d("===>senceSql:" + senceSql);

	}

	@Override
	public void onUpgrade(int oldVersion, int newVersion) {

	}
	
	public boolean insert(IconItem item) {
		ContentValues values = new ContentValues();
		values.put(SENCE_COLUMNS[COLUMN1], item.getTitle());
		values.put(SENCE_COLUMNS[COLUMN2], item.getFileName());
		values.put(SENCE_COLUMNS[COLUMN3], item.getDesc());
		long number = mSQLiteDatabase.insert(SENCE_TABLE, null, values);
		Logger.i("===>insert:"+number);
		return number == 1 ? true : false;
	}

	public boolean delete(IconItem item) {
		String[] whereArgs = { String.valueOf(item.getId()) };
		int number = mSQLiteDatabase.delete(SENCE_TABLE,
				SENCE_COLUMNS[COLUMN0]  +"=?", whereArgs);
		return number == 1 ? true : false;
	}

	public boolean update(IconItem item) {
		ContentValues values = new ContentValues();
		values.put(SENCE_COLUMNS[COLUMN1], item.getTitle());
		values.put(SENCE_COLUMNS[COLUMN2], item.getFileName());
		values.put(SENCE_COLUMNS[COLUMN3], item.getDesc());
		String[] whereArgs = { String.valueOf(item.getId()) };
		int number = mSQLiteDatabase.update(SENCE_TABLE, values, SENCE_COLUMNS[COLUMN0]+"=?", whereArgs);
		return number >= 1 ? true : false;
	}
	
	public List<IconItem> queryAll(){
		List<IconItem> items = new ArrayList<IconItem>();
		Cursor mCursor = mSQLiteDatabase.query(SENCE_TABLE, SENCE_COLUMNS, null, null, null, null, null);
		while(mCursor.moveToNext()){
			IconItem item = new IconItem();
			item.setId(mCursor.getInt(COLUMN0));
			item.setTitle(mCursor.getString(COLUMN1));
			item.setFileName(mCursor.getString(COLUMN2));
			item.setDesc(mCursor.getString(COLUMN3));
			items.add(item);
		}
		return items;
	}
}
